//
//  HRMessageToolBar.m
//  Hear
//
//  Created by mac on 2017/7/4.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRMessageToolBar.h"
#import <AVFoundation/AVFoundation.h>
#import "HRMeeageRecordView.h"
#import "UIButton+SSEdgeInsets.h"
@interface HRMessageToolBar ()<UITextViewDelegate,EMFaceDelegate,HRRecodBottomDelegate>
{
    CGFloat _previousTextViewContentHeight;//上一次inputTextView的contentSize.height
}
@property (nonatomic) CGFloat version;

@property (nonatomic,assign) DXKeyboardType keyboardType;


/**
 *  背景
 */
@property (strong, nonatomic) UIImageView *toolbarBackgroundImageView;
@property (strong, nonatomic) UIImageView *backgroundImageView;

/**
 *  按钮、输入框、toolbarView
 */
@property (strong, nonatomic) UIView *toolbarView;


//@property (strong, nonatomic) UIButton *recordButton;

@property (strong, nonatomic) UIButton *keyboardButton;
@property (strong, nonatomic) UIButton *moreButton;
@property (strong, nonatomic) UIButton *faceButton;
@property (strong, nonatomic) UIButton *photoButton;
@property (strong, nonatomic) UIButton *videoButton;


@property (strong, nonatomic) UIButton *locationButton;


@property (nonatomic,strong) UIView *topLineView;

@property (nonatomic,strong) UIView *buttomButtonParentView;

@property(nonatomic, strong)UIButton *openButton;


@property (strong, nonatomic) UIButton *tagButton;


/**
 *  底部扩展页面
 */
@property (nonatomic) BOOL isShowButtomView;
@property (strong, nonatomic) UIView *activityButtomView;//当前活跃的底部扩展页面
@property (strong, nonatomic) UIButton *recordButton;

@property (strong, nonatomic) UIButton *selectedButton;


@property(nonatomic, assign)NSInteger currentLocation;


@end
@implementation HRMessageToolBar


- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.keyboardType = DXKeyboardTypeVoice;
        [self setupConfigure];
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame keyboardType:(DXKeyboardType)keyboardType
{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.keyboardType = keyboardType;

        [self setupConfigure];
        
    }
    return self;
    
}

-(void)willMoveToSuperview:(UIView *)newSuperview
{
    // 当别的地方需要add的时候，就会调用这里
    if (newSuperview) {
        [self setupSubviews];
    }
    
    [super willMoveToSuperview:newSuperview];
}

- (void)didMoveToSuperview
{
    [super didMoveToSuperview];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    
    _delegate = nil;
    _inputTextView.delegate = nil;
    _inputTextView = nil;
}

#pragma mark - getter

- (UIImageView *)backgroundImageView
{
    if (_backgroundImageView == nil) {
        _backgroundImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        _backgroundImageView.backgroundColor = [UIColor clearColor];
        _backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    }
    
    return _backgroundImageView;
}

- (UIImageView *)toolbarBackgroundImageView
{
    if (_toolbarBackgroundImageView == nil) {
        _toolbarBackgroundImageView = [[UIImageView alloc] init];
        _toolbarBackgroundImageView.backgroundColor = [UIColor clearColor];
        _toolbarBackgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    
    return _toolbarBackgroundImageView;
}

- (UIView *)toolbarView
{
    if (_toolbarView == nil) {
        _toolbarView = [[UIView alloc] init];
        _toolbarView.backgroundColor = [UIColor clearColor];
    }
    
    return _toolbarView;
}

#pragma mark - setter

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    _backgroundImage = backgroundImage;
    self.backgroundImageView.image = backgroundImage;
}

- (void)setToolbarBackgroundImage:(UIImage *)toolbarBackgroundImage
{
    _toolbarBackgroundImage = toolbarBackgroundImage;
    self.toolbarBackgroundImageView.image = toolbarBackgroundImage;
}

- (void)setMaxTextInputViewHeight:(CGFloat)maxTextInputViewHeight
{
    if (maxTextInputViewHeight > kInputTextViewMaxHeight) {
        maxTextInputViewHeight = kInputTextViewMaxHeight;
    }
    _maxTextInputViewHeight = maxTextInputViewHeight;
}

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if ([self.delegate respondsToSelector:@selector(inputTextViewWillBeginEditing:)]) {
        [self.delegate inputTextViewWillBeginEditing:self.inputTextView];
    }
    
    self.recordButton.selected = NO;
    self.faceButton.selected = NO;
    self.photoButton.selected = NO;
    self.videoButton.selected = NO;
    self.keyboardButton.selected = YES;
    
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [textView becomeFirstResponder];
    
    if ([self.delegate respondsToSelector:@selector(inputTextViewDidBeginEditing:)]) {
        [self.delegate inputTextViewDidBeginEditing:self.inputTextView];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    self.keyboardButton.selected = NO;
    [textView resignFirstResponder];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {
        if ([self.delegate respondsToSelector:@selector(didSendText:)]) {
            [self.delegate didSendText:textView.text];
            self.inputTextView.text = @"";
            [self willShowInputTextViewToHeight:[self getTextViewContentH:self.inputTextView]];;
        }
        
        return NO;
    }
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView
{
    [self willShowInputTextViewToHeight:[self getTextViewContentH:textView]];
}

#pragma mark - DXFaceDelegate

- (void)selectedFacialView:(NSString *)str isDelete:(BOOL)isDelete
{
    NSMutableString *chatText = [[NSMutableString alloc] initWithString:self.inputTextView.text];
    
    if (!isDelete && str.length > 0) {
        if (chatText.length >0 && self.currentLocation <chatText.length){
            [chatText insertString:str atIndex:self.currentLocation];
            
            self.inputTextView.text = chatText;
            self.currentLocation = self.currentLocation+str.length;
        }else{
            self.inputTextView.text = [NSString stringWithFormat:@"%@%@",chatText,str];
        }
    }else {
        if (chatText.length >= 2)
        {
            if( [@"]" isEqualToString: [chatText substringFromIndex:chatText.length-1]]){
                
                NSRange range = [chatText rangeOfString:@"["
                                                options:NSBackwardsSearch];
                
                self.inputTextView.text = [chatText substringToIndex:range.location];
                
                if(self.keyboardType!=DXKeyboardTypeNoTextViewInput  &&self.keyboardType !=DXKeyboardTypeVoiceAndNoTextViewInput){
                    [self textViewDidChange:self.inputTextView];
                    
                }
                
                //                DebugLog(@"---] = %@",NSStringFromRange(range));
                return;
            }
        }
        
        [self.inputTextView deleteBackward];
        
    }
    if(self.keyboardType!=DXKeyboardTypeNoTextViewInput && self.keyboardType!=DXKeyboardTypeVoiceAndNoTextViewInput){
        [self textViewDidChange:self.inputTextView];
        
    }
}


- (void)sendFace
{
    NSString *chatText = self.inputTextView.text;
    if (chatText.length > 0) {
        if ([self.delegate respondsToSelector:@selector(didSendText:)]) {
            [self.delegate didSendText:chatText];
            self.inputTextView.text = @"";
            [self willShowInputTextViewToHeight:[self getTextViewContentH:self.inputTextView]];;
        }
    }
}

#pragma mark - UIKeyboardNotification

- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    void(^animations)() = ^{
        [self willShowKeyboardFromFrame:beginFrame toFrame:endFrame];
    };
    
    void(^completion)(BOOL) = ^(BOOL finished){
    };
    
    [UIView animateWithDuration:duration delay:0.0f options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:animations completion:completion];
}

#pragma mark - private

/**
 *  设置初始属性
 */
- (void)setupConfigure
{
    self.version = [[[UIDevice currentDevice] systemVersion] floatValue];
    
    self.maxTextInputViewHeight = kInputTextViewMaxHeight;
    
    self.activityButtomView = nil;
    self.isShowButtomView = NO;
    
    self.backgroundImageView.image = [UIImage imageWithColor:CustomColor(254, 254, 254,1)];
    
    [self addSubview:self.backgroundImageView];
    if(self.keyboardType==DXKeyboardTypeVoice){
        self.toolbarView.frame = CGRectMake(0, 0, self.frame.size.width,  kInputTextViewMinHeight);
    }else if (self.keyboardType==DXKeyboardTypeNoPhoto){
        self.toolbarView.frame = CGRectMake(0, 0, self.frame.size.width,  kInputTextViewMinHeight);
    }else if (self.keyboardType == DXKeyboardTypeNoTextViewInput || self.keyboardType == DXKeyboardTypeVoiceAndNoTextViewInput){
        self.toolbarView.frame = CGRectMake(0, 0, self.frame.size.width,  50+kBottomHeight);
        
        
    }else{
         self.toolbarView.frame = CGRectMake(0, 0, self.frame.size.width,  [HRMessageToolBar defaultInputTextViewHeight]);
    }
    
    self.toolbarBackgroundImageView.frame = self.toolbarView.bounds;
    [self.toolbarView addSubview:self.toolbarBackgroundImageView];
    [self addSubview:self.toolbarView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
}

- (void)setupSubviews
{
    CGFloat allButtonWidth = 0.0;
    //    CGFloat textViewLeftMargin = 6.0;
    
    
#pragma mark - 默认的样式，用于聊天
    if (self.keyboardType==DXKeyboardTypeVoice){
        
        [self createInputTextView];
        // 输入框的高度和宽度
        self.inputTextView.frame = CGRectMake(4, 0, kFullWidth-4*2, 36);
        self.inputTextView.placeHolder = @"说点什么吧...";
        [self.toolbarView addSubview:self.inputTextView];
        
        
        
        self.buttomButtonParentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.inputTextView.bottom,kFullWidth, 50)];
        [self.toolbarView addSubview:self.buttomButtonParentView];
        
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,0,  kFullWidth, 0.3)];
        lineView.backgroundColor = CustomColor(242, 242, 242,1);
        [self.buttomButtonParentView addSubview:lineView];
        
        
        
//        self.keyboardButton = [[UIButton alloc] initWithFrame:CGRectMake(paddingLeft, buttonY, buttonFrame, buttonFrame)];
//        [self.keyboardButton setImage:[UIImage imageNamed:@"chatBar_keyboard"] forState:UIControlStateNormal];
//        [self.keyboardButton setImage:[UIImage imageNamed:@"chatBar_keyboard_selected"] forState:UIControlStateSelected];
//        self.keyboardButton.tag = 1;
//        [self.keyboardButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.buttomButtonParentView addSubview:self.keyboardButton];
        
        
        //表情
        [self createFaceButton];
//        self.faceButton.frame = CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame);
        self.faceButton.frame = CGRectMake(paddingLeft, buttonY, buttonFrame, buttonFrame);
        self.faceButton.tag = 2;
        [self.faceButton setTitle:@"表情" forState:0];
        [self.faceButton setImagePositionWithType:SSImagePositionTypeTop spacing:0];
        [self.faceButton setTitleColor:FirstTextColor forState:0];
        self.faceButton.titleLabel.font = CustomUIFont(15);
        allButtonWidth = self.faceButton.right;
        [self.buttomButtonParentView addSubview:self.faceButton];
        
        
        //        录音
//        [self createVoiceButton];
//        self.recordButton.frame = CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame);
//        [self.buttomButtonParentView addSubview:self.recordButton];
//        self.recordButton.tag = 3;
        
//        allButtonWidth = self.recordButton.right;
        //        选择图片
        self.photoButton = [[UIButton alloc] initWithFrame:CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame)];
        [self.photoButton setImage:[UIImage imageNamed:@"chatBar_Photo"] forState:UIControlStateNormal];
        self.photoButton.tag = 4;
        [self.photoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttomButtonParentView addSubview:self.photoButton];
        allButtonWidth = self.recordButton.right;
        [self.photoButton setTitleColor:FirstTextColor forState:0];
        [self.photoButton setTitle:@"闪照" forState:0];
        self.photoButton.titleLabel.font = CustomUIFont(15);
        [self.photoButton setImagePositionWithType:SSImagePositionTypeTop spacing:0];
        // 录视频
//        self.videoButton = [[UIButton alloc] initWithFrame:CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame)];
//        [self.videoButton setImage:[UIImage imageNamed:@"chatBar_Video"] forState:UIControlStateNormal];
//        [self.videoButton setImage:[UIImage imageNamed:@"chatBar_Video_selected"] forState:UIControlStateSelected];
//        self.videoButton.tag = 5;
//        [self.videoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.buttomButtonParentView addSubview:self.videoButton];
//        allButtonWidth = self.videoButton.right;
//
//
//        // 发送地理位置
//        self.locationButton = [[UIButton alloc] initWithFrame:CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame)];
//        [self.locationButton setImage:[UIImage imageNamed:@"默认-位置"] forState:UIControlStateNormal];
//        [self.locationButton setImage:[UIImage imageNamed:@"默认-位置"] forState:UIControlStateSelected];
//        self.locationButton.tag = 6;
//        [self.locationButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.buttomButtonParentView addSubview:self.locationButton];
        
        
        
        if (!self.moreView) {
            self.moreView = [[EaseChatBarMoreView alloc] initWithFrame:CGRectMake(0, self.buttomButtonParentView.top, self.frame.size.width, 200) type:EMChatToolbarTypeGroup];
            
            self.moreView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        }
        
        
        if (!self.recordView) {
            self.recordView = [[HRRecordBottomView alloc] initWithFrame:CGRectMake(0, self.buttomButtonParentView.top, kFullWidth, 216)];
            self.recordView.delegate = self;
            
            [self createRecordView];
            
            
        }
        
        
        
        
        
    }else if(self.keyboardType==DXKeyboardTypeNoPhoto){
        [self createInputTextView];
        // 输入框的高度和宽度
        self.inputTextView.frame = CGRectMake(4, 0, kFullWidth-4*2, 36);
        self.inputTextView.placeHolder = @"说点什么吧...";
        [self.toolbarView addSubview:self.inputTextView];
        
        
        
        self.buttomButtonParentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.inputTextView.bottom,kFullWidth, 50)];
        [self.toolbarView addSubview:self.buttomButtonParentView];
        
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,0,  kFullWidth, 0.3)];
        lineView.backgroundColor = CustomColor(242, 242, 242,1);
        [self.buttomButtonParentView addSubview:lineView];
        
        
        
//        self.keyboardButton = [[UIButton alloc] initWithFrame:CGRectMake(paddingLeft, buttonY, buttonFrame, buttonFrame)];
//        [self.keyboardButton setImage:[UIImage imageNamed:@"chatBar_keyboard"] forState:UIControlStateNormal];
//        [self.keyboardButton setImage:[UIImage imageNamed:@"chatBar_keyboard_selected"] forState:UIControlStateSelected];
//        self.keyboardButton.tag = 1;
//        [self.keyboardButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.buttomButtonParentView addSubview:self.keyboardButton];
//        allButtonWidth = self.keyboardButton.right;
        
        
        //表情
        [self createFaceButton];
        //        self.faceButton.frame = CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame);
        self.faceButton.frame = CGRectMake(paddingLeft, buttonY, buttonFrame, buttonFrame);
        self.faceButton.tag = 2;
        
        allButtonWidth = self.faceButton.right;
        [self.buttomButtonParentView addSubview:self.faceButton];
        
        
        //        录音
        [self createVoiceButton];
        self.recordButton.frame = CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame);
        [self.buttomButtonParentView addSubview:self.recordButton];
        self.recordButton.tag = 3;
        
        allButtonWidth = self.recordButton.right;
        //        选择图片
//        self.photoButton = [[UIButton alloc] initWithFrame:CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame)];
//        [self.photoButton setImage:[UIImage imageNamed:@"chatBar_Photo"] forState:UIControlStateNormal];
//        [self.photoButton setImage:[UIImage imageNamed:@"chatBar_Photo_selected"] forState:UIControlStateSelected];
//        self.photoButton.tag = 4;
//        [self.photoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.buttomButtonParentView addSubview:self.photoButton];
//        allButtonWidth = self.photoButton.right;
        
//        // 录视频
//        self.videoButton = [[UIButton alloc] initWithFrame:CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame)];
//        [self.videoButton setImage:[UIImage imageNamed:@"chatBar_Video"] forState:UIControlStateNormal];
//        [self.videoButton setImage:[UIImage imageNamed:@"chatBar_Video_selected"] forState:UIControlStateSelected];
//        self.videoButton.tag = 5;
//        [self.videoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.buttomButtonParentView addSubview:self.videoButton];
        
        
        
        
        if (!self.moreView) {
            self.moreView = [[EaseChatBarMoreView alloc] initWithFrame:CGRectMake(0, self.buttomButtonParentView.top, self.frame.size.width, 200) type:EMChatToolbarTypeGroup];
            
            self.moreView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
        }
        
        
        if (!self.recordView) {
            self.recordView = [[HRRecordBottomView alloc] initWithFrame:CGRectMake(0, self.buttomButtonParentView.top, kFullWidth, 216)];
            self.recordView.delegate = self;

            
            [self createRecordView];
            
            
        }
        
        
    }else if (self.keyboardType == DXKeyboardTypeNoTextViewInput){
        
        self.buttomButtonParentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.inputTextView.bottom,kFullWidth, 50)];
        [self.toolbarView addSubview:self.buttomButtonParentView];
        
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,0,  kFullWidth, 0.3)];
        lineView.backgroundColor = CustomColor(242, 242, 242,1);
        [self.buttomButtonParentView addSubview:lineView];
        
        
        //表情
        [self createFaceButton];
        //        self.faceButton.frame = CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame);
        self.faceButton.frame = CGRectMake(paddingLeft, buttonY, buttonFrame, buttonFrame);
        self.faceButton.tag = 2;
        
        
        allButtonWidth = self.faceButton.right;
        [self.buttomButtonParentView addSubview:self.faceButton];
        
        
        [self createOpenButton];
        self.openButton.frame = CGRectMake(paddingLeft+allButtonWidth, buttonY, buttonFrame, buttonFrame);
        self.openButton.tag = 10;
        [self.buttomButtonParentView addSubview:self.openButton];
        allButtonWidth = self.openButton.right;

        
        [self createTagButton];
        self.tagButton.frame = CGRectMake(paddingLeft+allButtonWidth, buttonY, buttonFrame, buttonFrame);
        self.tagButton.tag = 11;
        [self.buttomButtonParentView addSubview:self.tagButton];
        

        
        //只有选择图片
        //        [self createSelectedPhoto];
        //        [self.toolbarView addSubview:self.photoButton];
        
    }else if (self.keyboardType == DXKeyboardTypeVoiceAndNoTextViewInput){
        self.buttomButtonParentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.inputTextView.bottom,kFullWidth, 50)];
        [self.toolbarView addSubview:self.buttomButtonParentView];
        
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0,0,  kFullWidth, 0.3)];
        lineView.backgroundColor = CustomColor(242, 242, 242,1);
        [self.buttomButtonParentView addSubview:lineView];
        
        
        //表情
        [self createFaceButton];
        //        self.faceButton.frame = CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame);
        self.faceButton.frame = CGRectMake(paddingLeft, buttonY, buttonFrame, buttonFrame);
        self.faceButton.tag = 2;
        
        
        allButtonWidth = self.faceButton.right;
        [self.buttomButtonParentView addSubview:self.faceButton];
        
        
        
        //        录音
        [self createVoiceButton];
        self.recordButton.frame = CGRectMake(allButtonWidth+buttonMagin, buttonY, buttonFrame, buttonFrame);
        [self.buttomButtonParentView addSubview:self.recordButton];
        self.recordButton.tag = 3;
        
        allButtonWidth = self.recordButton.right;
        
        
        
        if (!self.recordView) {
            self.recordView = [[HRRecordBottomView alloc] initWithFrame:CGRectMake(0, self.buttomButtonParentView.top, kFullWidth, 216)];
            self.recordView.delegate = self;
            
            
            [self createRecordView];
            
            
        }
    }
    
    
    if (!self.faceView) {
        
        if(self.keyboardType == DXKeyboardTypeNoTextViewInput ||self.keyboardType == DXKeyboardTypeVoiceAndNoTextViewInput){
            //            隐藏发送按钮
            self.faceView = [[EaseFaceView alloc] initWithFrame:CGRectMake(0, self.buttomButtonParentView.top, self.frame.size.width, 180) hiddenSendButton:YES];
//            _faceView = [[EaseFaceView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_toolbarView.frame), self.frame.size.width, 180)];
            [(EaseFaceView *)_faceView setDelegate:self];
            _faceView.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:242 / 255.0 blue:247 / 255.0 alpha:1.0];
            _faceView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
            
        }else{
//            self.faceView = [[EaseFaceView alloc] initWithFrame:CGRectMake(0, self.buttomButtonParentView.top, self.frame.size.width, 216)];
            _faceView = [[EaseFaceView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_toolbarView.frame), self.frame.size.width, 180) hiddenSendButton:NO];
            [(EaseFaceView *)_faceView setDelegate:self];
            _faceView.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:242 / 255.0 blue:247 / 255.0 alpha:1.0];
            _faceView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
            
        }
        
//        [(EaseFaceView *)self.faceView setDelegate:self];
//        
//        self.faceView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    }
    
    
    
    self.topLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.toolbarView.width, 0.3)];
    self.topLineView.backgroundColor = CustomColor(242, 242, 242,1);
    
    [self.toolbarView addSubview:self.topLineView];
    
#pragma mark - 背景颜色
    //    self.backgroundColor = [UIColor brownColor];
    //    self.toolbarView.backgroundColor = [UIColor grayColor];
    //    self.buttomButtonParentView.backgroundColor = [UIColor orangeColor];
    //
    
    
    
    
}

#pragma mark - 创建是否公开按钮
-(void)createOpenButton
{
    //表情
    self.openButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.faceButton.frame) - kInputTextViewMinHeight - kHorizontalPadding, kVerticalPadding, kInputTextViewMinHeight, kInputTextViewMinHeight)];
    self.openButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    [self.openButton setImage:[UIImage imageNamed:@"开锁"] forState:UIControlStateNormal];
    [self.openButton setImage:[UIImage imageNamed:@"闭锁"] forState:UIControlStateHighlighted];
    [self.openButton setImage:[UIImage imageNamed:@"闭锁"] forState:UIControlStateSelected];
    
    
    [self.openButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 创建标签按钮
-(void)createTagButton
{
    //表情
    self.tagButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.openButton.frame) - kInputTextViewMinHeight - kHorizontalPadding, kVerticalPadding, kInputTextViewMinHeight, kInputTextViewMinHeight)];
    self.tagButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    [self.tagButton setImage:[UIImage imageNamed:@"默认-标签"] forState:UIControlStateNormal];
    [self.tagButton setImage:[UIImage imageNamed:@"选中-标签"] forState:UIControlStateHighlighted];
    
    
    [self.tagButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}



#pragma mark - 创建表情按钮
-(void)createFaceButton
{
    //表情
    self.faceButton = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.moreButton.frame) - kInputTextViewMinHeight - kHorizontalPadding, kVerticalPadding, kInputTextViewMinHeight, kInputTextViewMinHeight)];
    self.faceButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
    [self.faceButton setImage:[UIImage imageNamed:@"chatBar_face"] forState:UIControlStateNormal];
    
    
    [self.faceButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 录音点击按钮
-(void)createVoiceButton
{
    self.recordButton = [[UIButton alloc] initWithFrame:CGRectMake(kHorizontalPadding, kVerticalPadding, kInputTextViewMinHeight, kInputTextViewMinHeight)];
    self.recordButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.recordButton setImage:[UIImage imageNamed:@"chatBar_record"] forState:UIControlStateNormal];
    [self.recordButton setImage:[UIImage imageNamed:@"chatBar_record_selected"] forState:UIControlStateSelected];
    [self.recordButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)createLocationButton
{
    self.locationButton = [[UIButton alloc] initWithFrame:CGRectMake(kHorizontalPadding, kVerticalPadding, kInputTextViewMinHeight, kInputTextViewMinHeight)];
    self.locationButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.locationButton setImage:[UIImage imageNamed:@"默认-位置"] forState:UIControlStateNormal];
    [self.locationButton setImage:[UIImage imageNamed:@"默认-位置"] forState:UIControlStateSelected];
    [self.locationButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}




#pragma mark - 初始化输入框
-(void)createInputTextView
{
    // 初始化输入框
    self.inputTextView = [[EaseTextView  alloc] init];
    self.inputTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    //    self.inputTextView.contentMode = UIViewContentModeCenter;
    _inputTextView.scrollEnabled = YES;
    _inputTextView.returnKeyType = UIReturnKeySend;
    _inputTextView.enablesReturnKeyAutomatically = YES; // UITextView内部判断send按钮是否可以用
    _inputTextView.placeHolder = self.placeHolder;
    _inputTextView.delegate = self;
    _inputTextView.backgroundColor = [UIColor clearColor];
    //    _inputTextView.layer.borderColor = [UIColor colorWithWhite:0.8f alpha:1.0f].CGColor;
    //    _inputTextView.layer.borderWidth = 0.65f;
    //    _inputTextView.layer.cornerRadius = 6.0f;
    _previousTextViewContentHeight = [self getTextViewContentH:_inputTextView];
}

#pragma makr - 点击显示录音
-(void)createRecordView
{
    
//    [self.recordView.centerImaegView addTarget:self action:@selector(recordButtonTouchDown) forControlEvents:UIControlEventTouchDown];
//    [self.recordView.centerImaegView addTarget:self action:@selector(recordButtonTouchUpOutside) forControlEvents:UIControlEventTouchUpOutside];
//    [self.recordView.centerImaegView addTarget:self action:@selector(recordButtonTouchUpInside) forControlEvents:UIControlEventTouchUpInside];
//    
//    
//    //离开当前按钮 UIControlEventTouchDragExit //
//    
//    [self.recordView.centerImaegView addTarget:self action:@selector(recordDragOutside:withEvent:) forControlEvents:UIControlEventTouchDragOutside];
//    //从外部进入到当前按钮UIControlEventTouchDragEnter
//    [self.recordView.centerImaegView addTarget:self action:@selector(recordDragInside) forControlEvents:UIControlEventTouchDragInside];
    
    
}

#pragma makr - 选择图片和照相的按钮
-(void)createSelectedPhoto
{
    
    self.photoButton = [[UIButton alloc] initWithFrame:CGRectMake(kHorizontalPadding, kVerticalPadding, kInputTextViewMinHeight, kInputTextViewMinHeight)];
    self.photoButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.photoButton setImage:[UIImage imageNamed:@"tupian"] forState:UIControlStateNormal];
    //    [self.photoButton setImage:[UIImage imageNamed:@"chatBar_keyboard"] forState:UIControlStateSelected];
    [self.photoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    //    self.photoButton.tag = -1;
    
    
    
}


#pragma makr - 选择图片和照相的按钮
-(void)createSelectedVideo
{
    
    self.videoButton = [[UIButton alloc] initWithFrame:CGRectMake(kHorizontalPadding, kVerticalPadding, kInputTextViewMinHeight, kInputTextViewMinHeight)];
    self.videoButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
    [self.videoButton setImage:[UIImage imageNamed:@"tupian"] forState:UIControlStateNormal];
    //    [self.photoButton setImage:[UIImage imageNamed:@"chatBar_keyboard"] forState:UIControlStateSelected];
    [self.videoButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
}








#pragma mark - change frame

- (void)willShowBottomHeight:(CGFloat)bottomHeight
{
    //    CGRect fromFrame = self.frame;
    CGFloat toHeight = self.toolbarView.height + bottomHeight;
//    DebugLog(@"---self.height = %f  toolView.height = %f ",self.height,self.toolbarView.height);
    CGFloat y = self.top + (self.height - toHeight);
    
    
    
    CGRect toFrame = CGRectMake(self.left, y, self.width, toHeight);
    
    
    //如果需要将所有扩展页面都隐藏，而此时已经隐藏了所有扩展页面，则不进行任何操作
    if(bottomHeight == 0 && self.height == self.toolbarView.height)
    {
        return;
    }
    
    if (bottomHeight == 0) {
        self.isShowButtomView = NO;
    }else{
        self.isShowButtomView = YES;
    }
    
    self.frame = toFrame;
    
    if (_delegate && [_delegate respondsToSelector:@selector(didChangeFrameToHeight:)]) {
        [_delegate didChangeFrameToHeight:toHeight];
    }
}

- (void)willShowBottomView:(UIView *)bottomView
{
    self.currentLocation = self.inputTextView.selectedRange.location;
    [self.inputTextView resignFirstResponder];

    if (![self.activityButtomView isEqual:bottomView]) {
        CGFloat bottomHeight = bottomView ? bottomView.frame.size.height : 0;
        [self willShowBottomHeight:bottomHeight];
        
        if (bottomView) {
            CGRect rect = bottomView.frame;
            rect.origin.y = CGRectGetMaxY(self.toolbarView.frame);
            bottomView.frame = rect;
            [self addSubview:bottomView];
        }
        
        if (self.activityButtomView) {
            [self.activityButtomView removeFromSuperview];
        }
        self.activityButtomView = bottomView;
    }
}

- (void)willShowKeyboardFromFrame:(CGRect)beginFrame toFrame:(CGRect)toFrame
{
    if (beginFrame.origin.y == [[UIScreen mainScreen] bounds].size.height)
    {
        //一定要把self.activityButtomView置为空
        [self willShowBottomHeight:toFrame.size.height];
        if (self.activityButtomView) {
            [self.activityButtomView removeFromSuperview];
        }
        self.activityButtomView = nil;
    }
    else if (toFrame.origin.y == [[UIScreen mainScreen] bounds].size.height)
    {
        [self willShowBottomHeight:0];
    }
    else{
        [self willShowBottomHeight:toFrame.size.height];
    }
}

- (void)willShowInputTextViewToHeight:(CGFloat)toHeight
{
    if (toHeight < 35) {
        toHeight = 35;
    }
    if (toHeight > self.maxTextInputViewHeight) {
        toHeight = self.maxTextInputViewHeight;
    }
    
    if (toHeight == _previousTextViewContentHeight)
    {
        return;
        
    }else{
        
        CGFloat changeHeight = toHeight - _previousTextViewContentHeight;
        
        CGRect rect = self.frame;
        rect.size.height += changeHeight;
        rect.origin.y -= changeHeight;
        self.frame = rect;
        
        
        rect = self.toolbarView.frame;
        rect.size.height += changeHeight;
        self.toolbarView.frame = rect;
        
        if (self.version < 7.0) {
            [self.inputTextView setContentOffset:CGPointMake(0.0f, (self.inputTextView.contentSize.height - self.inputTextView.frame.size.height) / 2) animated:YES];
        }
        _previousTextViewContentHeight = toHeight;
        self.buttomButtonParentView.top = self.inputTextView.bottom;
        
        if (_delegate && [_delegate respondsToSelector:@selector(didChangeFrameToHeight:)]) {
            [_delegate didChangeFrameToHeight:self.frame.size.height];
        }
    }
}

- (CGFloat)getTextViewContentH:(UITextView *)textView
{
    if (self.version >= 7.0)
    {
        return ceilf([textView sizeThatFits:textView.frame.size].height);
    } else {
        return textView.contentSize.height;
    }
}

#pragma mark - action

- (void)buttonAction:(UIButton *)button
{
    
    
    
    
    button.selected = !button.selected;
    //    self.selectedButton = button;
    
    
    if([self.delegate respondsToSelector:@selector(didSelectButton:)]){
        [self.delegate didSelectButton:button.selected];
    }
    
    switch (button.tag) {
        case 1://切换状态
        {
            if (button.selected) {
                //键盘也算一种底部扩展页面
                [self.inputTextView becomeFirstResponder];
                
            } else{
                
                //录音状态下，不显示底部扩展页面
                [self willShowBottomView:nil];
                
                //将inputTextView内容置空，以使toolbarView回到最小高度
                self.inputTextView.text = @"";
                [self textViewDidChange:self.inputTextView];
                [self.inputTextView resignFirstResponder];
                
            }
            
            
        }
            break;
        case 2://表情
        {
            if (button.selected) {
                self.recordButton.selected = NO;
                
                if (self.keyboardButton.selected) {
                    self.keyboardButton.selected = NO;
                    [self.inputTextView resignFirstResponder];
                    
                }
                
                [self willShowBottomView:self.faceView];
                
            } else {
                //显示键盘
                
                self.keyboardButton.selected = YES;
                [self.inputTextView becomeFirstResponder];
                
            }
        }
            break;
        case 3://录音
        {
            if (button.selected) {
                self.faceButton.selected = NO;
                
                if (self.keyboardButton.selected) {
                    self.keyboardButton.selected = NO;
                    [self.inputTextView resignFirstResponder];
                    
                }
                
                [self willShowBottomView:self.recordView];
                
            }else{
                //显示键盘
                
                [self.inputTextView becomeFirstResponder];
                self.keyboardButton.selected = YES;
            }
        }
            break;
#pragma mark - 选择照片按钮
        case 4:
        {
            button.selected = NO;
            [self.inputTextView resignFirstResponder];
            [self willShowBottomView:nil];
            if([self.delegate respondsToSelector:@selector(didSelectPhoto)]){
                [self.delegate didSelectPhoto];
            }
            
        }
            break;
            
#pragma mark - 录制视频按钮
        case 5:
        {
            // 判断相机权限是否打开
            NSString *mediaType = AVMediaTypeVideo;
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
            if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
                
                
            }else{
                button.selected = NO;
                [self.inputTextView resignFirstResponder];
                [self willShowBottomView:nil];
                if([self.delegate respondsToSelector:@selector(didSelectVideo)]){
                    [self.delegate didSelectVideo];
                }
            }
            
            
        }
            break;
        case 6:
        {
            button.selected = NO;
            [self.inputTextView resignFirstResponder];
            [self willShowBottomView:nil];
            if([self.delegate respondsToSelector:@selector(didSendLoation)]){
                [self.delegate didSendLoation];
            }
            
        }
            break;
#pragma mark - 是否公开帖

        case 10:
        {
            [self.inputTextView resignFirstResponder];
            [self willShowBottomView:nil];
            if([self.delegate respondsToSelector:@selector(didSelectOpen:)]){
                [self.delegate didSelectOpen:button.selected];
            }
            
        }
            break;
#pragma mark - 选择标签按钮
            
        case 11:
        {
            [self.inputTextView resignFirstResponder];
            [self willShowBottomView:nil];
            if([self.delegate respondsToSelector:@selector(didSelectTag)]){
                [self.delegate didSelectTag];
            }
            
        }
            break;
        default:
            break;
    }
    

    
    
}



#pragma mark - 录音事件
- (void)recordButtonTouchDown
{
    
    [self.recordView recordButtonTouchDown];
    if (_delegate && [_delegate respondsToSelector:@selector(didStartRecordingVoiceAction:)]) {
        [_delegate didStartRecordingVoiceAction:self.recordView.centerImaegView];
    }
    
}
#pragma mark -取消发送语音
- (void)recordButtonTouchUpOutside
{
    if (_delegate && [_delegate respondsToSelector:@selector(didCancelRecordingVoiceAction:)])
    {
        [_delegate didCancelRecordingVoiceAction:self.recordView.centerImaegView];
    }
    
    [self.recordView recordButtonTouchUpOutside];
    
}


#pragma mark -发送语音
- (void)recordButtonTouchUpInside
{

    
//    [self.recordView recordButtonTouchUpInside];
//    
//    if ([self.delegate respondsToSelector:@selector(didFinishRecoingVoiceAction:andRecordPath:)])
//    {
//        [self.delegate didFinishRecoingVoiceAction:self.recordView.centerImaegView andRecordPath:];
//    }
    
}

- (void)recordDragOutside:(UIButton *)sender withEvent:(UIEvent *)myEvent
{
    
    UITouch *touch = [[myEvent allTouches] anyObject];
    CGPoint point = [touch locationInView:self.recordView.centerImaegView];
    if (CGRectContainsPoint(self.recordView.centerImaegView.bounds, point)){
        [self.recordView recordButtonDragOutside];
        if ([self.delegate respondsToSelector:@selector(didDragOutsideAction:)])
        {
            [self.delegate didDragOutsideAction:self.recordView.centerImaegView];
        }
    }else if(CGRectContainsPoint(self.recordView.leftButton.bounds, point)){
        [self recordButtonTouchUpOutside];
    }else if(CGRectContainsPoint(self.recordView.rightButton.bounds, point)){
        [self recordButtonTouchUpOutside];
    }else{
        [self.recordView recordButtonDragOutside];
        if ([self.delegate respondsToSelector:@selector(didDragOutsideAction:)])
        {
            [self.delegate didDragOutsideAction:self.recordView.centerImaegView];
        }
    }
    
    
}

- (void)recordDragInside
{
    [self.recordView recordButtonDragInside];
    if ([self.delegate respondsToSelector:@selector(didDragInsideAction:)])
    {
        [self.delegate didDragInsideAction:self.recordView.centerImaegView];
    }
    
    
    
}

#pragma mark - public

/**
 *  停止编辑
 */
- (BOOL)endEditing:(BOOL)force
{
    BOOL result = [super endEditing:force];
    
    self.recordButton.selected = NO;
    self.faceButton.selected = NO;
    self.photoButton.selected = NO;
    self.videoButton.selected = NO;
    self.keyboardButton.selected = NO;
    [self willShowBottomView:nil];
    
    return result;
}

/**
 *  取消触摸录音键
 */
- (void)cancelTouchRecord
{
    //    self.recordButton.selected = NO;
    //    self.recordButton.highlighted = NO;
    if ([_recordView isKindOfClass:[HRMeeageRecordView class]]) {
        [(HRMeeageRecordView *)_recordView recordButtonTouchUpInside];
        [_recordView removeFromSuperview];
    }
}

+ (CGFloat)defaultHeight
{
    return  kInputTextViewMinHeight+kBottomHeight;
}

+ (CGFloat)defaultInputTextViewHeight
{
    return  36;
}
#pragma mark - HRRecodBottomDelegate
-(void)didFinishRecoingVoiceActionFromRecordBottomView:(NSString *)recordPath andDuration:(NSInteger)aDuration{
    if ([self.delegate respondsToSelector:@selector(didFinishRecoingVoiceAction:andRecordPath:andDuration:)])
    {
        [self.delegate didFinishRecoingVoiceAction:self.recordView.centerImaegView andRecordPath:recordPath andDuration:aDuration];
    }

}

- (void)didStartRecordingVoiceActionFromRecordBottomView{
    if (_delegate && [_delegate respondsToSelector:@selector(didStartRecordingVoiceAction:)]) {
        [_delegate didStartRecordingVoiceAction:self.recordView.centerImaegView];
    }
}
- (void)didCancelRecordingVoiceActionFromRecordBottomView{
    if (_delegate && [_delegate respondsToSelector:@selector(didCancelRecordingVoiceAction:)]) {
        [_delegate didCancelRecordingVoiceAction:self.recordView.centerImaegView];
    }
}






@end
